enforce exclusivity among sort options (#970)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Mon, 2 Jan 2023 19:56:02 +0000 (12:56 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Jan 2023 19:56:02 +0000 (12:56 -0700)
* enforce exclusive sort options.

* use default brace inits for sort members.

sort.cc
sort.h

diff --git a/sort.cc b/sort.cc
index 6c4afecd93b272b7d911e5cc1c17c8eebf5fa0c7..058df4f443c7ce65ab74ab3710d1d9623fbc0f34 100644 (file)
--- a/sort.cc
+++ b/sort.cc
@@ -70,93 +70,99 @@ bool SortFilter::sort_comp_rh_by_number(const route_head* a, const route_head* b
 
 void SortFilter::process()
 {
-  if (wpt_sort_mode != SortModeWpt::none) {
-    switch (wpt_sort_mode) {
-    case SortModeWpt::description:
-      waypt_sort(sort_comp_wpt_by_description);
-      break;
-    case SortModeWpt::gcid:
-      waypt_sort(sort_comp_wpt_by_gcid);
-      break;
-    case SortModeWpt::shortname:
-      waypt_sort(sort_comp_wpt_by_shortname);
-      break;
-    case SortModeWpt::time:
-      waypt_sort(sort_comp_wpt_by_time);
-      break;
-    default:
-      fatal(MYNAME ": unknown waypoint sort mode.");
-    }
+  switch (wpt_sort_mode) {
+  case SortModeWpt::none:
+    break;
+  case SortModeWpt::description:
+    waypt_sort(sort_comp_wpt_by_description);
+    break;
+  case SortModeWpt::gcid:
+    waypt_sort(sort_comp_wpt_by_gcid);
+    break;
+  case SortModeWpt::shortname:
+    waypt_sort(sort_comp_wpt_by_shortname);
+    break;
+  case SortModeWpt::time:
+    waypt_sort(sort_comp_wpt_by_time);
+    break;
+  default:
+    fatal(MYNAME ": unknown waypoint sort mode.");
   }
 
-  if (rte_sort_mode != SortModeRteHd::none) {
-    switch (rte_sort_mode)  {
-    case SortModeRteHd::description:
-      route_sort(sort_comp_rh_by_description);
-      break;
-    case SortModeRteHd::name:
-      route_sort(sort_comp_rh_by_name);
-      break;
-    case SortModeRteHd::number:
-      route_sort(sort_comp_rh_by_number);
-      break;
-    default:
-      fatal(MYNAME ": unknown route sort mode.");
-    }
+  switch (rte_sort_mode)  {
+  case SortModeRteHd::none:
+    break;
+  case SortModeRteHd::description:
+    route_sort(sort_comp_rh_by_description);
+    break;
+  case SortModeRteHd::name:
+    route_sort(sort_comp_rh_by_name);
+    break;
+  case SortModeRteHd::number:
+    route_sort(sort_comp_rh_by_number);
+    break;
+  default:
+    fatal(MYNAME ": unknown route sort mode.");
   }
-  if (trk_sort_mode != SortModeRteHd::none) {
-    switch (trk_sort_mode)  {
-    case SortModeRteHd::description:
-      track_sort(sort_comp_rh_by_description);
-      break;
-    case SortModeRteHd::name:
-      track_sort(sort_comp_rh_by_name);
-      break;
-    case SortModeRteHd::number:
-      track_sort(sort_comp_rh_by_number);
-      break;
-    default:
-      fatal(MYNAME ": unknown track sort mode.");
-    }
+
+  switch (trk_sort_mode)  {
+  case SortModeRteHd::none:
+    break;
+  case SortModeRteHd::description:
+    track_sort(sort_comp_rh_by_description);
+    break;
+  case SortModeRteHd::name:
+    track_sort(sort_comp_rh_by_name);
+    break;
+  case SortModeRteHd::number:
+    track_sort(sort_comp_rh_by_number);
+    break;
+  default:
+    fatal(MYNAME ": unknown track sort mode.");
   }
 }
 
 void SortFilter::init()
 {
   // sort waypts by
-  if (opt_sm_description) {
+  if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
+    wpt_sort_mode = SortModeWpt::none;
+  } else if (opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
     wpt_sort_mode = SortModeWpt::description;
-  }
-  if (opt_sm_gcid) {
+  } else if (!opt_sm_description && opt_sm_gcid && !opt_sm_shortname && !opt_sm_time) {
     wpt_sort_mode = SortModeWpt::gcid;
-  }
-  if (opt_sm_shortname) {
+  } else if (!opt_sm_description && !opt_sm_gcid && opt_sm_shortname && !opt_sm_time) {
     wpt_sort_mode = SortModeWpt::shortname;
-  }
-  if (opt_sm_time) {
+  } else if (!opt_sm_description && !opt_sm_gcid && !opt_sm_shortname && opt_sm_time) {
     wpt_sort_mode = SortModeWpt::time;
+  } else {
+    fatal(MYNAME ": At most one of the options description, gcid, shortname and time may be selected.");
   }
 
   // sort routes by
-  if (opt_sm_rtedesc) {
+  if (!opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) {
+    rte_sort_mode = SortModeRteHd::none;
+  } else if (opt_sm_rtedesc && !opt_sm_rtename && !opt_sm_rtenum) {
     rte_sort_mode = SortModeRteHd::description;
-  }
-  if (opt_sm_rtename) {
+  } else if (!opt_sm_rtedesc && opt_sm_rtename && !opt_sm_rtenum) {
     rte_sort_mode = SortModeRteHd::name;
-  }
-  if (opt_sm_rtenum) {
+  } else if (!opt_sm_rtedesc && !opt_sm_rtename && opt_sm_rtenum) {
     rte_sort_mode = SortModeRteHd::number;
+  } else {
+    fatal(MYNAME ": At most one of the options rtedesc, rtename and rtenum may be selected.");
   }
 
   // sort tracks by
-  if (opt_sm_trkdesc) {
+  if (!opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) {
+    trk_sort_mode = SortModeRteHd::none;
+  } else if (opt_sm_trkdesc && !opt_sm_trkname && !opt_sm_trknum) {
     trk_sort_mode = SortModeRteHd::description;
-  }
-  if (opt_sm_trkname) {
+  } else if (!opt_sm_trkdesc && opt_sm_trkname && !opt_sm_trknum) {
     trk_sort_mode = SortModeRteHd::name;
-  }
-  if (opt_sm_trknum) {
+  } else if (!opt_sm_trkdesc && !opt_sm_trkname && opt_sm_trknum) {
     trk_sort_mode = SortModeRteHd::number;
+  } else {
+    fatal(MYNAME ": At most one of the options trkdesc, trkname and trknum may be selected.");
   }
 }
 
diff --git a/sort.h b/sort.h
index 07de160c7d0c890b00fc81a288bd5ae51526b806..81d64a660818c520372ee7df2dffa4d588cf41ea 100644 (file)
--- a/sort.h
+++ b/sort.h
 #include <QString>   // for QString
 #include <QVector>   // for QVector
 
-#include "defs.h"    // for ARGTYPE_BOOL, ARG_NOMINMAX, arglist_t, ARG_TERMI...
+#include "defs.h"    // for arglist_t, ARGTYPE_BOOL, ARG_NOMINMAX, Waypoint
 #include "filter.h"  // for Filter
 
+
 #if FILTERS_ENABLED
 
 class SortFilter:public Filter
 {
 public:
+
+  /* Member Functions */
+
   QVector<arglist_t>* get_args() override
   {
     return &args;
@@ -41,6 +45,9 @@ public:
   void process() override;
 
 private:
+
+  /* Types */
+
   enum class SortModeWpt {
     none,
     description,
@@ -49,8 +56,6 @@ private:
     time
   };
 
-  SortModeWpt wpt_sort_mode = SortModeWpt::none;       /* How are we sorting these? */
-
   enum class SortModeRteHd {
     none,
     description,
@@ -58,12 +63,32 @@ private:
     number
   };
 
-  SortModeRteHd rte_sort_mode = SortModeRteHd::none;   /* How are we sorting these? */
-  SortModeRteHd trk_sort_mode = SortModeRteHd::none;   /* How are we sorting these? */
+  /* Member Functions */
+
+  static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b);
+  static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b);
+  static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b);
+  static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b);
+  static bool sort_comp_rh_by_description(const route_head* a, const route_head* b);
+  static bool sort_comp_rh_by_name(const route_head* a, const route_head* b);
+  static bool sort_comp_rh_by_number(const route_head* a, const route_head* b);
+
+  /* Data Members */
 
-  char* opt_sm_gcid{}, *opt_sm_shortname{}, *opt_sm_description{}, *opt_sm_time{};
-  char* opt_sm_rtenum{}, *opt_sm_rtename{}, *opt_sm_rtedesc{};
-  char* opt_sm_trknum{}, *opt_sm_trkname{}, *opt_sm_trkdesc{};
+  SortModeWpt wpt_sort_mode{SortModeWpt::none};        /* How are we sorting these? */
+  SortModeRteHd rte_sort_mode{SortModeRteHd::none};    /* How are we sorting these? */
+  SortModeRteHd trk_sort_mode{SortModeRteHd::none};    /* How are we sorting these? */
+
+  char* opt_sm_gcid{};
+  char* opt_sm_shortname{};
+  char* opt_sm_description{};
+  char* opt_sm_time{};
+  char* opt_sm_rtenum{};
+  char* opt_sm_rtename{};
+  char* opt_sm_rtedesc{};
+  char* opt_sm_trknum{};
+  char* opt_sm_trkname{};
+  char* opt_sm_trkdesc{};
 
   QVector<arglist_t> args = {
     {
@@ -108,14 +133,6 @@ private:
     },
   };
 
-  static bool sort_comp_wpt_by_description(const Waypoint* a, const Waypoint* b);
-  static bool sort_comp_wpt_by_gcid(const Waypoint* a, const Waypoint* b);
-  static bool sort_comp_wpt_by_shortname(const Waypoint* a, const Waypoint* b);
-  static bool sort_comp_wpt_by_time(const Waypoint* a, const Waypoint* b);
-  static bool sort_comp_rh_by_description(const route_head* a, const route_head* b);
-  static bool sort_comp_rh_by_name(const route_head* a, const route_head* b);
-  static bool sort_comp_rh_by_number(const route_head* a, const route_head* b);
-
 };
 #endif // FILTERS_ENABLED
 #endif // SORT_H_INCLUDED_